package org.grow.auth.service;

import org.grow.auth.dao.UserInfoMapper;
import org.grow.auth.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.filter.CharacterEncodingFilter;

import java.nio.charset.CharacterCodingException;

/**
 * @Author: xwg
 * @CreateDate: 2022/5/25
 */

@Service
public class UserInfoService implements UserDetailsService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("===step2: " + username);
        UserInfo userInfo = userInfoMapper.findByUsername(username);
        if (userInfo == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        System.out.println("===step2: " + userInfo);

        UserDetails build = User.withUsername(userInfo.getUsername())
                .password(userInfo.getPassword())
                .disabled(!userInfo.getEnabled())
                .accountLocked(false)
                .credentialsExpired(false)
                .accountExpired(false)
                .authorities("xx", "yy")
                .build();
        System.out.println(build);
        return build;
    }
}
